package com.wanglei.distributed


import java.io.ObjectOutputStream
import java.net.Socket

object Driver {

  def main(args: Array[String]): Unit = {
    // 连接executor1
    val client1 = new Socket("localhost", 9000)
    val stream1 = client1.getOutputStream

    // 连接executor2
    val client2 = new Socket("localhost", 9001)
    val stream2 = client2.getOutputStream


    // 拆分任务
    val task = new Task

    val subTask1 = new SubTask
    subTask1.logic = task.logic
    subTask1.datas = task.datas.take(2)

    val subTask2 = new SubTask
    subTask2.logic = task.logic
    subTask2.datas = task.datas.takeRight(2)

    // 发送计算任务
    val objStream1 = new ObjectOutputStream(stream1)
    objStream1.writeObject(subTask1)
    objStream1.flush()
    println("driver给executor1发送数据完毕")


    val objStream2 = new ObjectOutputStream(stream2)
    objStream2.writeObject(subTask2)
    objStream2.flush()
    println("driver给executor2发送数据完毕")

    // 关闭资源
    objStream1.close()
    client1.close()
    objStream2.close()
    client2.close()

  }

}
